<?php
namespace app\admin\controller;

use app\common\builder\ZBuilder;
use app\admin\model\Banner as BannerModel;
use think\Image;
use think\File;
use think\facade\Hook;
use think\Db;
use think\facade\Env;

/**
 * 商家
 * @package app\admin\controller
 */
class Banner extends Admin
{
    public function index()
    {
        // 查询
        $map = $this->getMap();
        // 数据列表
        $bannerModel = new BannerModel();
        $data_list = $bannerModel->getList($map);
        // $data_list = BannerModel::where($map)->order('sort asc,id desc')->paginate();
        foreach($data_list as $k=>$row){
            $data_list[$k]['images'] = getImgUrl($row['images']);
        }

        $wxappList = Db::name('admin_wxapp')->field('id,title')->select();
        foreach($wxappList as $k=>$row){
            $wxlist[$row['id']] = $row['title'];
        }
        $fields = [
            ['hidden','id'],
            ['text','title','标题'],
            ['select','wxapp_id','选择小程序','',$wxlist],
            ['image','images','图片上传'],
            ['text','url','小程序跳转链接'],
            ['number','sort','排序'],
            ['hidden','addtime',time(),time()],
            ['radio','status','状态','', ['禁用', '启用'], 1],
        ];
        return ZBuilder::make('table')
        ->setSearch(['b.title' => '标题','w.title' => '所属小程序']) // 设置搜索框
        ->setPageTitle('广告图')
        //->addTopButton('add') // 添加顶部按钮
        ->autoAdd($fields,'admin_banner','Banner','addtime') // 添加新增按钮
        ->autoEdit($fields,'admin_banner','Banner','mdtime') // 添加编辑按钮
        ->addColumns([ // 批量添加数据列
            ['id', 'ID'],
            ['title', '标题'],
            ['wtitle', '所属小程序'],
            ['images', '图片',parse_attr(Db::name('admin_config')->where('name', 'upload_driver')->value('options'))],
            ['url', '跳转链接'],
            ['addtime', '添加时间', 'datetime'],
            ['status', '状态', 'switch'],
            ['right_button', '操作', 'btn']
        ])
        ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
        ->addRightButtons('delete') // 批量添加右侧按钮
        ->setRowList($data_list) // 设置表格数据
        ->fetch(); // 渲染模板
    }
}